name: Upload LangKit python Packages

on:
  release:
    types: [released]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Install Poetry
        uses: abatilo/actions-poetry@v2.0.0
        with:
          poetry-version: 1.6.1
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.8"
          cache: poetry

      - name: Install dependencies
        run: poetry install

      - name: Autobump version
        run: |
          # from refs/tags/v1.2.3 get 1.2.3
          VERSION=$(echo $GITHUB_REF | sed 's#.*/v##')
          echo $VERSION
          git config --local user.email "langkit@whylabs.ai"
          git config --local user.name "github-actions[bot]"
          poetry run bump2version patch --verbose --allow-dirty --commit --no-tag --new-version $VERSION

      #push version bump to tagged branch so new version is included in pypi
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          force: true

      - name: Build and publish LangKit to PyPI
        run: poetry publish --build --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}

  bump_mainline:
    runs-on: ubuntu-latest
    needs: deploy
    steps:
      - uses: actions/checkout@v3
      - name: Install Poetry
        uses: abatilo/actions-poetry@v2.0.0
        with:
          poetry-version: 1.6.1
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.8"
          cache: poetry

      - name: Install dependencies
        run: poetry install
      - name: read current version
        run: |
          # from refs/tags/v1.2.3 get 1.2.3
          echo "VERSION=$(echo $GITHUB_REF | sed 's#.*/v##')" >> $GITHUB_ENV
      - name: bump2version main to new release version
        run: poetry run bump2version patch --verbose --allow-dirty --no-tag --new-version ${{ env.VERSION }}
      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.VERSION_UPDATE_W0_AUTOMATOR }}
          commit-message: Update version to ${{ env.VERSION }}
          branch: update_release_version_${{ env.VERSION }}
          base: main
          delete-branch: true
          title: "Bump version to ${{ env.VERSION }}"
          committer: w0-automator <whylabs-automator@users.noreply.github.com>
          author: w0-automator <whylabs-automator@users.noreply.github.com>
